package com.hotel.domain.service.impl;

import com.github.pagehelper.PageHelper;
import com.hotel.common.PageEntity;
import com.hotel.domain.dao.RoomInfoMapper;
import com.hotel.domain.dao.RoomMapper;
import com.hotel.domain.service.RoomInfoService;
import com.hotel.pojo.Room;
import com.hotel.pojo.RoomInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoomInfoImpl implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private RoomMapper roomMapper;

    @Override
    @Transactional
    public int addRoomInfo(RoomInfo roomInfo) {
        Room room = roomInfo.getRoom();
        room.setRoomStatus("空房");
        return roomMapper.addRoom(room);
    }

    @Override
    @Transactional
    public int updateRoomInfo(RoomInfo roomInfo) {
        return roomMapper.updateRoom(roomInfo.getRoom());
    }

    @Override
    public PageEntity<RoomInfo> getRoomInfoList(RoomInfo roomInfo, int page, int perPage) {
        PageHelper.startPage(page, perPage);
        List<RoomInfo> list = roomInfoMapper.getRoomInfoList(roomInfo);
        int pageTotal = (int)Math.ceil(roomInfoMapper.getRoomInfoNum(roomInfo)/(double)perPage);
        return new PageEntity<RoomInfo>(list, page, pageTotal);
    }

}
